//
//  UserOrderLogisticsShipInfo.m
//  SmallElephant
//
//  Created by Faith on 2024/11/24.
//

#import "UserOrderLogisticsShipInfo.h"

@interface UserOrderLogisticsShipInfo ()

/// 物流订单信息
@property (nonatomic, strong) UIView *shipOrder;

/// 物流轨迹信息
@property (nonatomic, strong) UIView *shipOrbitInfo;

/// 显示隐藏更多按钮
@property (nonatomic, strong) UIButton *btnShipOrbitMoreLess;

/// 物流地址信息
@property (nonatomic, strong) UIView *shipAddressInfo;

/// 是否显示更多
@property (nonatomic, assign) BOOL showMore;

@end

@implementation UserOrderLogisticsShipInfo

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
    }
    return self;
}

/// 设置组件视图
- (void)setupViews {
    // 物流订单信息
    [self addSubview:self.shipOrder style:@"display: flex; flex-direction: column; background: #FFFFFF; margin-top: 10px; padding: 15px; padding-top: 10px;"];
    // 物流轨迹信息
    [self addSubview:self.shipOrbitInfo style:@"display: flex; flex-direction: column; background: #FFFFFF; padding: 15px; padding-top: 0px;"];
    // 分割线
    UIView *lineView = [[UIView alloc] init];
    [self addSubview:lineView style:@"display: flex; background: #E1E1E1; height: 1px; "];
    // 物流收货地址信息
    [self addSubview:self.shipAddressInfo style:@"display: flex; flex-direction: row; background: #FFFFFF; padding: 15px; "];
    
}

- (UIView *)shipOrbitInfo {
    if(!_shipOrbitInfo) {
        _shipOrbitInfo = [[UIView alloc] init];
    }
    return _shipOrbitInfo;
}

/// 懒加载物流订单信息
- (UIView *)shipOrder {
    if(!_shipOrder) {
        _shipOrder = [[UIView alloc] init];
        _shipOrder.layer.cornerRadius = 8;
        _shipOrder.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
        _shipOrder.layer.masksToBounds = YES;
        
        // 右边目的地信息
        UIView *orderInfo = [[UIView alloc] init];
        [_shipOrder addSubview:orderInfo style:@"display: flex; flex-direction: row; justify-content: flex-end; "];
        
        // 运单号标题
        UILabel *orderTitle = [[UILabel alloc] init];
        orderTitle.text = @"运单号";
        [orderInfo addSubview:orderTitle style:@"display: flex; flex: 0; font-size: 14px; color: #333333; "];
        
        // 运单号值
        UILabel *orderValue = [[UILabel alloc] init];
        [orderInfo addSubview:orderValue style:@"display: flex; flex: 1; font-size: 14px; color: #333333; margin-left: 10px;"];
        
        // 复制按钮
        UIButton *btnCopy = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnCopy setTitle:@"复制" forState:UIControlStateNormal];
        [orderInfo addSubview:btnCopy style:@"display: flex; flex: 0; font-size: 14px; color: #333333; margin-left: 10px; padding-left: 10px; padding-right: 10px;"];
        
        // 物流信息
        UIView *shipInfo = [[UIView alloc] init];
        [_shipOrder addSubview:shipInfo style:@"display: flex; flex-direction: row; background: #F5F5F5; border-radius: 8px; margin-top: 5px; padding: 15px;"];
        
        // 左边状态信息
        UIView *statusInfo = [[UIView alloc] init];
        [shipInfo addSubview:statusInfo style:@"display: flex; flex: 0; flex-direction: column;"];
        // 状态
        UILabel *shipStatus = [[UILabel alloc] init];
        shipStatus.text = @"已签收";
        [statusInfo addSubview:shipStatus style:@"display: flex; font-size: 14px; font-weight: bold; color: #333333; "];
        // 签收时间
        UILabel *shipTime = [[UILabel alloc] init];
        shipTime.text = @"签收时间: 09月23日 10:36";
        [statusInfo addSubview:shipTime style:@"display: flex; font-size: 12px; color: #666666; margin-top: 5px;"];
        
        // 右边目的地信息
        UIView *placeInfo = [[UIView alloc] init];
        [shipInfo addSubview:placeInfo style:@"display: flex; flex: 1; flex-direction: row; justify-content: flex-end;"];
        
        // 始发地
        UILabel *fromPlace = [[UILabel alloc] init];
        fromPlace.text = @"伊洛瓦底";
        fromPlace.numberOfLines = 1;
        [placeInfo addSubview:fromPlace style:@"display: flex; font-size: 14px; font-weight: bold; color: #333333; margin:5px; padding: 1px;"];
        
        // 连接线
        UIImageView *placeLinkIcon = [UIImageView withImage:@"icon-place-link@3x"];
        [placeInfo addSubview:placeLinkIcon style:@"display: flex; width: 20px; height:8px; align-self: center;"];
        
        // 目的地
        UILabel *toPlace = [[UILabel alloc] init];
        toPlace.text = @"昆明";
        toPlace.numberOfLines = 1;
        [placeInfo addSubview:toPlace style:@"display: flex; font-size: 14px;font-weight: bold; color: #333333; margin:5px; padding: 1px;"];
        
    }
    return _shipOrder;
}

/// 懒加载物流收货人信息
- (UIView *)shipAddressInfo {
    if(!_shipAddressInfo) {
        _shipAddressInfo = [[UIView alloc] init];
        
        // 收件人图标
        UIImageView *receiveIcon = [UIImageView withImage:@"icon-receiver@3x"];
        [_shipAddressInfo addSubview:receiveIcon style:@"display: flex; width: 18px; width: 18px; height:18px; align-self: flex-start;"];
        
        // 地址信息
        UIView *addressInfo = [[UIView alloc] init];
        [_shipAddressInfo addSubview:addressInfo style:@"display: flex; flex-direction: column; margin-left: 10px;"];
        
        // 详细地址
        UILabel *addressDetail = [[UILabel alloc] init];
        addressDetail.text = @"送至 商博街蓝湾俊园8栋2005";
        [addressInfo addSubview:addressDetail style:@"display: flex; font-size: 14px; font-weight: bold; color: #333333;"];
        
        // 联系信息
        UILabel *contactInfo = [[UILabel alloc] init];
        contactInfo.text = @"王先生 86-138****5689";
        [addressInfo addSubview:contactInfo style:@"display: flex; margin-top: 2px; font-size: 13px; color: #666666;"];
        
    }
    return _shipAddressInfo;
}

/// 设置物流订单数据
- (void)setShipOrderNumber:(NSString *)shipOrderNumber {
    UILabel *shipOrderNumberView = self.shipOrder.subviews[0].subviews[1];
    shipOrderNumberView.text = [NSString stringWithFormat:@"%@", shipOrderNumber];
}

/// 设置物流轨迹信息
- (void)setShipOrbitData:(NSArray *)orbitList {
    for(int i=0; i<orbitList.count; i++) {
        NSDictionary *orbitInfo = orbitList[i];
        UIView *orbitItem = [[UIView alloc] init];
        
        // 轨迹连接线
        UIView *lineInfo = [[UIView alloc] init];
        [orbitItem addSubview:lineInfo style:@"display: flex; flex-direction: column; flex: 0; width: 15px;"];
        // 图标
        UIImageView *lineIcon = [UIImageView withImage:i==0 ? @"icon-ship-now@3x" : @"icon-ship-passed@3x"];
        [lineInfo addSubview:lineIcon style:@"display: flex; width: 14px; height:14px; align-self: flex-start;"];
        // 连接线
        PHDashLineView *lineView = [[PHDashLineView alloc] init];
        [lineView addDashedBorderToView:[UIColor colorWithHexString:@"#CDCDCD"] andLineWidth: 1];
        [lineInfo addSubview:lineView style:@"display: flex; width: 1px; flex-grow: 1; align-self: center;"];
        
        // 轨迹详细信息
        UIView *detailInfo = [[UIView alloc] init];
        [orbitItem addSubview:detailInfo style:@"display: flex; flex: 1; flex-direction: column; margin-left: 10px; margin-top: -3px; padding-bottom: 15px;"];
        if(orbitInfo[@"status"]) {
            // 状态信息
            UILabel *statusInfo = [[UILabel alloc] init];
            statusInfo.text = [NSString stringWithFormat:@"%@", orbitInfo[@"status"]];
            if(i==0) {
                [detailInfo addSubview:statusInfo style:@"display: flex; font-size: 15px; font-weight: bold; color: #EF2020;"];
            } else {
                [detailInfo addSubview:statusInfo style:@"display: flex; font-size: 15px; font-weight: bold; color: #888888;"];
            }
        }
        // 时间信息
        UILabel *timeInfo = [[UILabel alloc] init];
        timeInfo.text = [NSString stringWithFormat:@"%@", orbitInfo[@"time"]];
        [detailInfo addSubview:timeInfo style:@"display: flex; margin-top: 2px; font-size: 14px; color: #666666;"];
        // 轨迹简介
        UILabel *summaryInfo = [[UILabel alloc] init];
        summaryInfo.numberOfLines = 0;
        summaryInfo.text = [NSString stringWithFormat:@"%@", orbitInfo[@"summary"]];
        [detailInfo addSubview:summaryInfo style:@"display: flex; margin-top: 2px; font-size: 14px; color: #666666;"];
        if(i>0) {
            [self.shipOrbitInfo addSubview:orbitItem style:@"display: none; flex-direction: row;"];
        } else {
            [self.shipOrbitInfo addSubview:orbitItem style:@"display: flex; flex-direction: row;"];
        }
    }
    // 显示更多按钮
    UIView *orbitItem = [[UIView alloc] init];
    // 图标
    UIImageView *lineIcon = [UIImageView withImage:@"icon-ship-passed@3x"];
    [orbitItem addSubview:lineIcon style:@"display: flex; width: 14px; height:14px;"];
    // 显示更多物流信息
    _btnShipOrbitMoreLess = [UIButton buttonWithType:UIButtonTypeCustom];
    [_btnShipOrbitMoreLess setTitle:@"显示更多物流信息" forState:UIControlStateNormal];
    [_btnShipOrbitMoreLess setTitle:@"收起更多物流信息" forState:UIControlStateSelected];
    [_btnShipOrbitMoreLess addTarget:self action:@selector(btnShowMoreMoreClickEventHandler) forControlEvents:UIControlEventTouchUpInside];
    [orbitItem addSubview:_btnShipOrbitMoreLess style:@"display: flex; font-size: 12px; height: 14px; color: #888888; margin-left: 10px;"];
    [self.shipOrbitInfo addSubview:orbitItem style:@"display: flex; flex-direction: row;"];
}

/// 显示收起更多显示
- (void)btnShowMoreMoreClickEventHandler {
    if(self.showMore) {
        self.showMore = NO;
        self.btnShipOrbitMoreLess.selected = NO;
        for(int i=0;i< self.shipOrbitInfo.subviews.count; i++) {
            UIView *itemView = self.shipOrbitInfo.subviews[i];
            if(i==0 || i==self.shipOrbitInfo.subviews.count - 1) {
                [itemView applyStyle:@"display: flex; flex-direction: row;"];
            } else {
                [itemView applyStyle:@"display: none; flex-direction: row;"];
            }
        }
    } else {
        self.showMore = YES;
        self.btnShipOrbitMoreLess.selected = YES;
        for(int i=0;i< self.shipOrbitInfo.subviews.count; i++) {
            UIView *itemView = self.shipOrbitInfo.subviews[i];
            [itemView applyStyle:@"display: flex; flex-direction: row;"];
        }
    }
    // 整个页面重新计算布局
    [[self getRootController] viewWillLayoutSubviews];
}

@end
